package com.clothing.controller.app;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.clothing.bean.UserJson;
import com.clothing.controller.BaseController;
import com.clothing.entity.Friend;
import com.clothing.entity.Friend.FriendStatus;
import com.clothing.entity.Friend.Status;
import com.clothing.entity.User;
import com.clothing.service.FriendService;
import com.clothing.service.UserService;
import com.clothing.utils.AjaxMsg;
import com.clothing.utils.page.Page;
import com.clothing.utils.page.Pageable;

/**
 * @ClassName: FriendController 
 * @Description: TODO(Controller-好友) 
 * @author Charles.Tao 
 * @date 2017年9月18日 下午1:59:48 
 * @version 1.0
 */
@RequestMapping("/app/friend")
@Controller("appFriendController")
public class FriendController extends BaseController
{
    @Autowired
    private UserService userService;
    
    @Autowired
    private FriendService friendService;
    
    /**
     * @Title: attention 
     * @Description: TODO(关注) 
     * @param @param userId
     * @param @return 设定文件 
     * @return String 返回类型 
     * @throws
     */
    @RequestMapping(value = "/attention", method = RequestMethod.POST)
    @ResponseBody
    public String attention(Long userId)
    {
        User user = userService.find(userId);
        
        if (null == user)
        {
            return  getParameterErrorMsg();
        }
        
        User current = userService.getCurrent();
        
        if (userId.equals(current.getId()))
        {
            return AjaxMsg.failed("不能关注自己");
        }
        
        Friend friend = friendService.findFriend(userId, current.getId());
        
        if (null == friend)
        {
            friend = new Friend();

            if (current.getId() > user.getId())
            {
                friend.setSmallUser(user);
                friend.setBigUser(current);
                friend.setStatus(Status.big_to_small);
            }
            else
            {
                friend.setSmallUser(current);
                friend.setBigUser(user);
                friend.setStatus(Status.small_to_big);
            }
        
            friendService.save(friend);
        }
        else
        {
            boolean isBig = friend.isBig(current);
            Status status = friend.getStatus();
            
            if (isBig)
            {
                if (Status.big_to_small == status || Status.mutual == status)
                {
                    return AjaxMsg.success();
                }
                else
                {
                    friend.setStatus(Status.mutual);
                }
            }
            else
            {
                if (Status.small_to_big == status || Status.mutual == status)
                {
                    return AjaxMsg.success();
                }
                else
                {
                    friend.setStatus(Status.mutual);
                }
            }
            
            friendService.update(friend);
        }
        
        return AjaxMsg.success();
    }
    
    /**
     * @Title: cancel 
     * @Description: TODO(取消关注) 
     * @param @param userId
     * @param @return 设定文件 
     * @return String 返回类型 
     * @throws
     */
    @RequestMapping(value = "/cancel", method = RequestMethod.POST)
    @ResponseBody
    public String cancel(Long userId)
    {
        User user = userService.find(userId);
        
        if (null == user)
        {
            return  getParameterErrorMsg();
        }
        
        User current = userService.getCurrent();
        
        if (userId.equals(current.getId()))
        {
            return AjaxMsg.failed("不能取消关注自己");
        }
        
        Friend friend = friendService.findFriend(userId, current.getId());
        
        if (null == friend)
        {
            return getParameterErrorMsg();
        }
        
        boolean isBig = friend.isBig(current);
        Status status = friend.getStatus();
        
        if (isBig)
        {
            if (Status.big_to_small == status)
            {
                friendService.delete(friend);
            }
            else if (Status.mutual == status)
            {
                friend.setStatus(Status.small_to_big);
                friendService.update(friend);
            }
            else
            {
                return getParameterErrorMsg();
            }
        }
        else
        {
            if (Status.small_to_big == status)
            {
                friendService.delete(friend);
            }
            else if (Status.mutual == status)
            {
                friend.setStatus(Status.big_to_small);
                friendService.update(friend);
            }
            else
            {
                return getParameterErrorMsg();
            }
        }
        
        return AjaxMsg.success();
    }
    
    /**
     * @Title: list 
     * @Description: TODO(好友列表) 
     * @param @param pageable
     * @param @return 设定文件 
     * @return String 返回类型 
     * @throws
     */
    @RequestMapping(value = "/getList.json", method = RequestMethod.GET)
    @ResponseBody
    public String list(FriendStatus status, Pageable pageable)
    {
        if (null == status || FriendStatus.stranger == status)
        {
            return getParameterErrorMsg();
        }
        
        Long userId = userService.getCurrentId();
        Page<Friend> page = friendService.findFriends(userId, status, pageable);
        JSONArray ja = null;
        
        if (page.getTotal() > 0)
        {
            ja = new JSONArray();
            
            for (Friend friend : page.getContent())
            {
                User smallUser = friend.getSmallUser();
                User bigUser = friend.getBigUser();
                User user = null;
                
                if (smallUser.getId().equals(userId))
                {
                    user = bigUser;
                }
                else
                {
                    user = smallUser;
                }
                
                UserJson json = new UserJson();
                json.setUserId(user.getId());
                json.setNickname(user.getNickname());
                json.setHeadImgUrl(user.getHeadImgUrl());
                ja.add(json);
            }
        }
        
        return new AjaxMsg(true, page.getTotal(), ja).toString();
    }
}
